// 测试D组抽签功能
const API_BASE = 'http://localhost:5000';

async function testDGroup() {
    console.log('🧪 开始测试D组抽签功能...\n');
    
    try {
        // 1. 重置数据
        console.log('1. 重置数据...');
        const resetResponse = await fetch(`${API_BASE}/api/reset`, {
            method: 'POST'
        });
        const resetResult = await resetResponse.json();
        console.log('✓ 数据重置成功:', resetResult.message);
        
        // 2. 获取配置信息
        console.log('\n2. 获取配置信息...');
        const configResponse = await fetch(`${API_BASE}/api/config`);
        const config = await configResponse.json();
        console.log('✓ 配置信息:', config);
        console.log('✓ 支持的分组:', config.groups);
        
        // 3. 进行多次抽签，直到抽到D组或达到最大尝试次数
        console.log('\n3. 进行抽签测试，寻找D组...');
        let attempts = 0;
        let foundDGroup = false;
        const maxAttempts = 20;
        
        while (attempts < maxAttempts && !foundDGroup) {
            attempts++;
            const drawResponse = await fetch(`${API_BASE}/api/draw`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ name: `测试队长${attempts}` })
            });
            
            const drawResult = await drawResponse.json();
            if (drawResult.success) {
                console.log(`✓ 队长${attempts} 抽中 ${drawResult.group} 组`);
                if (drawResult.group === 'D') {
                    foundDGroup = true;
                    console.log('🎉 成功抽到D组！前端应该能正常显示结果弹窗。');
                }
            } else {
                console.log(`❌ 队长${attempts} 摇号失败:`, drawResult.error);
                break;
            }
            
            // 添加小延迟
            await new Promise(resolve => setTimeout(resolve, 200));
        }
        
        if (!foundDGroup && attempts >= maxAttempts) {
            console.log('⚠️  经过多次尝试未抽到D组，但这是正常的随机结果。');
            console.log('   可以手动在前端测试D组的显示效果。');
        }
        
        // 4. 检查当前分组状态
        console.log('\n4. 检查当前分组状态...');
        const groupsResponse = await fetch(`${API_BASE}/api/groups`);
        const currentGroups = await groupsResponse.json();
        
        console.log('当前分组情况:');
        Object.keys(currentGroups).forEach(group => {
            console.log(`  ${group}组: ${currentGroups[group].length} 人 - ${currentGroups[group].join(', ')}`);
        });
        
        console.log('\n🎉 测试完成！');
        console.log('现在可以在前端 http://localhost:5173/ 测试：');
        console.log('1. 尝试抽签看是否能正常显示所有组的结果');
        console.log('2. 特别注意D组是否能正常显示而不会白屏');
        
    } catch (error) {
        console.error('❌ 测试失败:', error.message);
    }
}

testDGroup();
